-- random_trap
-- create by panyl
-- 随机型陷阱

return {
    getTargets = function(trapId, pos, targetPos)
        local info = CommonTrapM.query(trapId);
        local trapType = info["type"];
        local feature = info["feature"];

        assert(trapType == TRAP_TYPE_RANDOM, "不是随机型的陷阱！");

        local count = info["radius"] or 0;
        count = math.min(count, GRID_SIZE);

        local ret = {};
        local grids = {};

        for index = 1, GRID_SIZE do
            repeat
                if index == pos then
                    break;
                end

                local grid = DungeonM.getGridByPos(index);
                local monster = grid.monster;

                -- 是否无视障碍的
                if  not grid:isOpened() and
                    bit.band(feature, TRAP_FEATURE_IGNORE_BLOCK) ~= TRAP_FEATURE_IGNORE_BLOCK then
                    break;
                end

                if  not grid:isMonster() or
                    not monster or
                    monster:isDead() then
                    break;
                end

                table.insert(grids, grid);
            until true;
        end

        local n = #grids;
        count = math.min(count, n);

        -- 随机取出count个格子
        while count > 0 do
            count = count - 1;

            local rand = DungeonM.getRandSeed("random_trap");
            local index = rand % n + 1;

            table.insert(ret, grids[index]);
            grids[index] = grids[n];
            n = n - 1;
        end

        return ret;
    end,
};
